﻿using SmartPlant.Model;
using SmartPlant.Model.DbContext.Biz;
using SmartPlant.Services.Interfaces;
using SqlSugar;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace SmartPlant.Services
{
    /// <summary>
    /// 对应TaskJob控制器
    /// </summary>
    public class TaskJobService : BaseService, ITaskJobService
    {
        /// <summary>
        /// 查询TaskJob表里的记录
        /// </summary>
        /// <param name="proId"></param>
        /// <returns></returns>
        public async Task<TaskJob> GetJobAsync(string proId, string taskId)
        {
            string uri = $"/api/Project/{proId}/task-job-api/{taskId}";
            var res = await GetAsync<TaskJob>(uri);
            return res.data;
        }

        /// <summary>
        /// 查询TaskJob表里的记录（所有的）
        /// </summary>
        /// <param name="proId"></param>
        /// <returns></returns>
        public async Task<List<TaskJob>> GetLisAsync(string proId)
        {
            string uri = $"/api/Project/{proId}/Task-Job-Api";
            var res = await GetAsync<List<TaskJob>>(uri);
            return res.data;
        }
        /// <summary>
        /// 文件下载
        /// </summary>
        /// <param name="proId"></param>
        /// <param name="taskJob"></param>
        /// <returns></returns>
        public async Task<RESTfulResult<Stream>> GetFileAsync(string proId, TaskJob taskJob)
        {
            string url = $"/api/Project/{proId}/task-job-api/file-download-client/{taskJob.ID}";
            var result = await PostBodyStreamAsync(url, taskJob.ID);//其实body里不需要东西
            return result;
        }

        /// <summary>
        /// 文件下载，以新的<see cref="FileContentResultBaseString"/>格式
        /// </summary>
        /// <param name="proId"></param>
        /// <param name="task"><see cref="TaskJob"/>对象</param>
        /// <returns></returns>
        public async Task<RESTfulResult<FileContentResultBaseString>> DownloadFileAsync(string proId, string taskid)
        {
            string url = $"/api/Project/{proId}/task-job-api/file-download-client/{taskid}";
            var furionRes = await PostAsync<FileContentResultBaseString>(url);
            return furionRes;
        }

        /// <summary>
        /// 查询TaskJob表里的记录（所有的）
        /// </summary>
        /// <param name="proId"></param>
        /// <returns></returns>
        public async Task<RESTfulResult<string>> DeleteTaskJobAsync(string proId,string taskid)
        {
            string uri = $"/api/Project/{proId}/Task-Job-Api/{taskid}";
            var res = await DeleteAsync<string>(uri);
            return res;
        }
    }
}
